﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Text;
using Modules.Core;
using Modules.Library.Entities;
using MySql.Data.MySqlClient;

namespace ComputerStoreManager {
    public partial class Connection {

        public bool UpdateToDatabase(string strInsert) {
            if (m_connection.State == ConnectionState.Closed) {
                if (!this.Connect()) {
                    return false;
                }
            }
            MySqlCommand myCommand = new MySqlCommand(strInsert, m_connection);

            bool checkExcuteNonQuery = true;
            try {
                myCommand.ExecuteNonQuery();
            }
            catch (Exception ex) {
                Error = ex.Message;
                checkExcuteNonQuery = false;
            }

            Disconnect();
            return checkExcuteNonQuery;
        }

        public DataTable GetDataTable(string sql) {
            DataSet dataSet = null;
            try {
                MySqlCommand selectCommand = new MySqlCommand(sql, m_connection);
                MySqlDataAdapter adapter = new MySqlDataAdapter(selectCommand);
                dataSet = new DataSet();
                adapter.Fill(dataSet);
                if (dataSet.Tables.Count > 0) {
                    return dataSet.Tables[0];
                }
            }
            catch (Exception ex) {
                Error = "Loi khi lay du lieu" + ex.Message;
            }
            return null;

        }

        public bool InsertNewData(object obj, KindOfObject kind) {
            string query;
            if (m_connection.State == System.Data.ConnectionState.Open) {
                switch (kind) {
                    case KindOfObject.BY_CUSTOMER:
                        query = "insert into khachhang values(" + Converter.GetStringInsert(obj, kind) + ")";
                        if (!UpdateToDatabase(query)) {
                            Error = "Loi khi insert du lieu";
                            return false;
                        }
                        else {
                            UpdateId(KindOfObject.BY_CUSTOMER, (obj as Customer).MaKhachHang);
                            return true;
                        }
                    case KindOfObject.BY_EMPLOYEE:
                        query = "insert into nhanvien values(" + Converter.GetStringInsert(obj, kind) + ")";
                        if (!UpdateToDatabase(query)) {
                            Error = "Loi khi insert dữ liệu";
                            return false;
                        }
                        else {
                            UpdateId(KindOfObject.BY_EMPLOYEE, (obj as Employee).MaNhanVien);
                            return true;
                        }
                    case KindOfObject.BY_PRODUCT:
                        query = "insert into linhkien values(" + Converter.GetStringInsert(obj, kind) + ")";
                        if (!UpdateToDatabase(query)) {
                            Error = "Lỗi khi insert dữ liệu";
                            return false;
                        }
                        else {
                            UpdateId(KindOfObject.BY_PRODUCT, (obj as Product).MaMatHang);
                            return true;
                        }
                    case KindOfObject.BY_INVOICE:
                        query = "insert into hoadonban values(" + Converter.GetStringInsert(obj, kind) + ")";
                        if (!UpdateToDatabase(query)) {
                            Error = "Lỗi khi insert dữ liệu";
                            return false;
                        }
                        else {
                            UpdateId(KindOfObject.BY_INVOICE, (obj as Invoice).MaHoaDonBan);
                            return true;
                        }
                    case KindOfObject.BY_INVOICEDETAILS:
                        query = "insert into chitiethoadonban(MaHDBan,MaMH,GiaBan,SoLuongBan,MaKM,TongTien) values(" + Converter.GetStringInsert(obj, kind) + ")";
                        if (!UpdateToDatabase(query))
                        {
                            Error = "Lỗi khi insert dữ liệu";
                            return false;
                        }
                        else
                        {
                            UpdateId(KindOfObject.BY_INVOICEDETAILS, (obj as InvoiceDetail).MaCTHDBan);
                            return true;
                        }
                }
            }
            Error = "Loi khi insert du lieu";
            return false;
        }
        public bool UpdateData(object obj, KindOfObject kind) {
            string query;
            if (m_connection.State == System.Data.ConnectionState.Open) {
                switch (kind) {
                    case KindOfObject.BY_CUSTOMER:
                        query = "update khachhang set " + Converter.GetStringUpdate(obj, kind) + " " + " where MaKH='" + (obj as Customer).MaKhachHang+"'";
                        if (!UpdateToDatabase(query)) {
                            Error = "Loi khi insert du lieu";
                            return false;
                        }
                        else {
                            //UpdateId(KindOfObject.BY_CUSTOMER, (obj as Customer).MaKhachHang);
                            return true;
                        }
                    case KindOfObject.BY_EMPLOYEE:
                        query = "update nhanvien set " + Converter.GetStringUpdate(obj, kind) + " " + " where MaNV='" + (obj as Employee).MaNhanVien + "'";
                        if (!UpdateToDatabase(query)) {
                            Error = "Loi khi insert dữ liệu";
                            return false;
                        }
                        else {
                            //UpdateId(KindOfObject.BY_EMPLOYEE, (obj as Employee).MaNhanVien);
                            return true;
                        }
                    case KindOfObject.BY_PRODUCT:
                        query = "update linhkien set " + Converter.GetStringUpdate(obj, kind) + " " + " where MaMH='" + (obj as Product).MaMatHang + "'";
                        if (!UpdateToDatabase(query)) {
                            Error = "Lỗi khi insert dữ liệu";
                            return false;
                        }
                        else {
                            //UpdateId(KindOfObject.BY_PRODUCT, (obj as Product).MaMatHang);
                            return true;
                        }
                    //case KindOfObject.BY_INVOICE:
                    //    query = "";
                    //    if (!UpdateToDatabase(query)) {
                    //        Error = "Lỗi khi insert dữ liệu";
                    //        return false;
                    //    }
                    //    else {
                    //        //UpdateId(KindOfObject.BY_INVOICE, (obj as Invoice).MaHoaDonBan);
                    //        return true;
                    //    }
                    //case KindOfObject.BY_INVOICEDETAILS:
                    //    query = "";
                    //    if (!UpdateToDatabase(query))
                    //    {

                    //        return false;
                    //    }
                    //    else
                    //    {
                    //        return true;
                    //    }
                }
            }
            Error = "Loi khi update du lieu";
            return false;
        }

        public bool DeleteCustomer(Customer cus) {
            if (m_connection.State == System.Data.ConnectionState.Open) {
            }
            Error = "Co loi trong qua trinh delete";
            return false;
        }

        public bool DeleteInvoice(Invoice invoice) {
            if (m_connection.State == System.Data.ConnectionState.Open) {
            }
            Error = "Co loi trong qua trinh delete";
            return false;
        }

        private void UpdateId(KindOfObject kind, string id) {
            string query;
            switch (kind) {
                case KindOfObject.BY_EMPLOYEE:
                    query = "update thongsophu set MaNhanVien=" + "'" + id + "'";
                    UpdateToDatabase(query);
                    break;
                case KindOfObject.BY_CUSTOMER:
                    query = "update thongsophu set MaKhachHang=" + "'" + id + "'";
                    UpdateToDatabase(query);
                    break;
                case KindOfObject.BY_PRODUCT:
                    query = "update thongsophu set MaLinhKien=" + "'" + id + "'";
                    UpdateToDatabase(query);
                    break;
                case KindOfObject.BY_INVOICE:
                    query = "update thongsophu set MaHoaDon=" + "'" + id + "'";
                    UpdateToDatabase(query);
                    break;
            }
        }

    }
}
